﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delonix_Regia_HMS
{
    public partial class ControlPanel : Form
    {
        public ControlPanel()
        {
            InitializeComponent();
            string type = InitializeLogin();
            if (type != null)
            {
                CustomerManagement cmForm = new CustomerManagement();
                cmForm.TopLevel = false;
                panel1.Controls.Add(cmForm);
                cmForm.Show();

                RoomAvailabilityBooking rabForm = new RoomAvailabilityBooking();
                rabForm.TopLevel = false;
                panel2.Controls.Add(rabForm);
                rabForm.Show();

                CheckInOut cioForm = new CheckInOut();
                cioForm.TopLevel = false;
                panel3.Controls.Add(cioForm);
                cioForm.Show();

                HousekeepingStaffManagement hsmForm = new HousekeepingStaffManagement();
                hsmForm.TopLevel = false;
                panel4.Controls.Add(hsmForm);
                hsmForm.Show();

                Reporting rForm = new Reporting(type);
                rForm.TopLevel = false;
                panel5.Controls.Add(rForm);
                rForm.Show();

                UserAccounts uaForm = new UserAccounts();
                uaForm.TopLevel = false;
                panel6.Controls.Add(uaForm);
                uaForm.Show();
            }
        }

        public string InitializeLogin()
        {
            this.Hide();
            Login loginDialog = new Login();
            loginDialog.ShowDialog();
            string type = loginDialog.staffType;
            lblStaffType.Text = type;
            switch (type)
            {
                case "Administrator":
                    break;
                case "Manager":
                    tabControl1.TabPages.Remove(tabPage6);
                    break;
                case "Receptionist":
                    tabControl1.TabPages.Remove(tabPage4);
                    tabControl1.TabPages.Remove(tabPage6);
                    break;
            }
            loginDialog.Dispose();

            return type;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearComboBoxes(this.Controls);
            ClearTextBoxes(this.Controls);
            ClearListView(this.Controls);
        }

        private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    tb.Text = "";
                }

                else
                {
                    ClearTextBoxes(ctrl.Controls);
                }
            }
        }

        private void ClearComboBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                ComboBox cb = ctrl as ComboBox;
                if (cb != null)
                {
                    cb.Text = "";
                }

                else
                {
                    ClearComboBoxes(ctrl.Controls);
                }
            }
        }

        private void ClearListView(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                ListView lv = ctrl as ListView;
                if (lv != null)
                {
                    lv.Items.Clear();
                }

                else
                {
                    ClearListView(ctrl.Controls);
                }
            }
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to logout?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                this.Hide();
                ControlPanel cpDialog = new ControlPanel();
                cpDialog.Show();
            }
        }

        private void ControlPanel_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
